home *** CD-ROM | disk | FTP | other *** search
- %
- #EF
- #T15,1,Chapter 5 General Design Considerations Pg. 14
- #HS,1,4,80,25,11,1
- #C4,R5
- ~W~IThe Mysterious Magic Number~Y~I
-
- In addition to the other things we've discussed in this chapter, it's also
- a good idea to avoid what are called ~W~Imagic numbers~Y~I. A magic number is a
- number in program code that is not self-explanatory. Most numbers aren't
- self-explanatory. Here's an example of a loop containing a magic number.
-
-
- ~W~IFigure 5.4~Y~I
- A loop with a magic number
-
-
- for (i=0;i<~W~I35~Y~I;i++) /* Why does the loop go to 35? You */
- { /* can't tell just by looking at it. */
- /* Code goes in here. */
- .
- .
- .
- }
-
- #WN
- #BO,4,8,78,12,7,1,0,2,15,6
- Can you tell me what that loop does? Probably not. Now let's look at the
- same loop with a named constant used to replace the magic number.
-
- #WP
- %
- #EF
- #T15,1,Chapter 5 General Design Considerations Pg. 15
- #HS,1,4,80,25,11,1
- #C4,R5
- ~W~IFigure 5.5~Y~I
- The same loop without the magic number
-
- for (i=0;i<|MAX_STUDENTS_PER_CLASS|;i++)
- {
- /* Code goes in here. */
- .
- .
- .
- }
- #WN
- #C4,R17
- Now do you know what it does? In the first example, the number 35 could have
- meant ~W~Ianything~Y~I. But anyone with a little experience in C can look at the
- second example and see that it does some sort of processing on a classroom
- of students. The loop isn't seen as running from 0 to 34. We now see it as
- running from an empty class to a full class. The latter is much clearer and
- ~G~Imore closely models the real world~Y~I.
-
- #WP
- %
- #EF
- #T15,1,Chapter 5 General Design Considerations Pg. 16
- #HS,1,4,80,25,11,1
- #C4,R5
- ~Y~IEven if we only use a magic number once in a given program, it is wise
- to define a named constant for it. Using named constants helps make the
- code self-documenting.
-
- #WN
- Staying away from magic numbers also helps the program to be more
- modifiable. Imagine that I wrote a program using the loop in Figure 5.4.
- Now let's ask ourselves what would happen if I were to have several dozen
- more loops in the same program, all of which ran from 0 to 34. The school
- board meets and decides that the maximum number of students per class is
- now 37.
-
- #WN
- ~KThe software must now be changed. If I have used the literal number
- 35 in several dozen loops, I must go back and change ~R~Iall of those loops~Y~I. If
- I have used a named constant as the upper limit of all those loops, I only
- have to go back and ~G~Ichange the number associated with the named constant~Y~I.
- When I re-compile, it will be changed everywhere the named constant appears.
- In other words, ~C~II've found another way to be lazy.~Y~I~k
-
- #WP
- %
- #EF
- #T15,1,Chapter 5 General Design Considerations Pg. 17
- #HS,1,4,80,25,11,1
- #C4,R5
- ~Y~I
- With the combination of building proper programs from proper program
- segments, information hiding, implementation hiding, and avoiding magic
- numbers, we can put together robust programs that are composed of highly
- reusable pieces. If we can reuse the pieces, we are being successfully
- lazy.
- #WP
- #X
-